﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ECommerce.BLL.Member;
using Jzy.Core.Common.Web;
using Jzy.Core.Common;
using Jzy.Common;
using Jzy.Core.Common.Basic;
using ECommerce.Model.Member;
using Jzy.Core.Utility;

namespace ECommerce.Web.ECAdmin.Member
{
    public partial class List : PageList
    {
        private MemberBaseBO Member
        {
            get
            {
                return MemberBaseBO.Instance<MemberBaseBO>();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (!string.IsNullOrEmpty(Request.QueryString["Name"]))
                {
                    txtName.Text = Request.QueryString["Name"];
                }
                BindView();
            }
            base.InitSelectID(hidSelectIDs);
            AddAttr();
        }

        private void BindView()
        {
            LVUtil.Init(lv, lvHeader);
            PagerSetting setting = new PagerSetting();
            setting.PageIndex = this.PageIndex;
            setting.PageSize = AppConfig.PageSize;
            setting.OrderBy = "InputTime desc";
            if (txtName.Text.Trim() != "")
            {
                setting.Where = "Name like @p0 or LoginName like @p0 or Mobile like @p0 or Email like @p0";
            }

            string prefix = GlobalVar.WebCurrPath + "List.aspx?Name=" + Server.UrlEncode(txtName.Text);
            List<MemberBaseInfo> list = this.Member.GetList(setting, "%" + txtName.Text + "%");
            lv.DataKeyNames = new string[] { "ID" };
            lv.DataSource = list;
            lv.DataBind();

            litPager.Text = HtmlPager.Render(setting, prefix, true);
            
        }

        private void AddAttr()
        {
            JSUtil.ShowForm(lnkAdd, "增加会员", null, GlobalVar.WebCurrPath + "Edit.aspx", 400, 382);
            JSUtil.ClickBtn(lnkDelete, btnDelete);
            JSUtil.ClickBtn(lnkStartup, btnStartup);
            JSUtil.ClickBtn(lnkStop, btnStop);
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            BindView();
        }
        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            BindView();
        }
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (strSelectIDs != "")
            {
                this.Member.Delete(StrUtil.SplitToStr(strSelectIDs));                
            }
            BindView();
        }

        protected void lv_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                LVUtil.SetEdit(sender, e, 400, 382);
               
            }
        }

        protected void btnStartup_Click(object sender, EventArgs e)
        {
            List<int> lstMemberIDs = StrUtil.SplitToInt(strSelectIDs);
            this.Member.Startup(lstMemberIDs);
            BindView();
        }
        protected void btnStop_Click(object sender, EventArgs e)
        {
            List<int> lstMemberIDs = StrUtil.SplitToInt(strSelectIDs);
            Member.Stop(lstMemberIDs);
            BindView();
        }
    }
}